package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.Options;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  2021-12-28 15:47:16
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class OptionsDao extends BaseDao<Options> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<Options> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }


    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<Options> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<Options> list = run.query(sql, new ResultSetHandler<List<Options>>() {
            @Override
            public List<Options> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    Options options = new Options();

                    options.setId(rs.getInt("id"));
                    options.setKey(rs.getString("key_"));
                    options.setType(rs.getInt("type"));
                    options.setValue(rs.getString("value"));

                    list.add(options);
                }
                return list;
            }
        },params);

        return list;
    }

    /**
     *
     * @param id 查询主键
     * @return  Options 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public Options selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select id, key_, type, value from mto_options where id = ?";
        Options options = run.query(sql,new ResultSetHandler<Options>() {
            @Override
            public Options handle(ResultSet rs) throws SQLException {
                Options options = null;
                if(rs.next()) {
                    options = new Options();

                        options.setId(rs.getInt("id"));
                        options.setKey(rs.getString("key_"));
                        options.setType(rs.getInt("type"));
                        options.setValue(rs.getString("value"));
                }
                return  options;
            }
        },params);
        return options;
    }

    /**
    *
    * @param options 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(Options options) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO mto_options (  ");
        sql.append("  key_, type, value ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("       ?,  ?,  ?  ");
        sql.append("   )  ");

        Object[] params = {
            
            options.getKey(),  
            options.getType(),  
            options.getValue()
        };

        return run.update(sql.toString(),params);
    }


    /**
    * 根据主键更新
    * @param  options
    * @return
    * @throws SQLException
    */
    @Override
    public int update(Options options) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   mto_options           ");
        sql.append(" SET                    ");
            
            sql.append("   key_  = ?,  ");
            sql.append("   type  = ?,  ");
            sql.append("   value  = ?");

        sql.append(" WHERE id= ?            ");

        Object[] params = {
                
                options.getKey(), 
                options.getType(), 
                options.getValue(), 
            options.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from mto_options where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

}
